package cn.wolfcode.luowowo.website.web.util;

import cn.wolfcoe.luowowo.common.exception.LogicException;
import cn.wolfcoe.luowowo.common.util.Consts;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class CookieUtil {
    public static  void addCookie(HttpServletResponse response, String token) {
        Cookie cookie = new Cookie( "token", token );
        cookie.setPath( "/" );
        cookie.setMaxAge( Consts.USER_INFO_TOKEN_VAI_TIME * 60 );
        response.addCookie( cookie );

    }

    public static String getToken(HttpServletRequest request,HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length != 0) {
            String token = "";
            for (Cookie cookie : cookies) {
                if ("token".equals( cookie.getName())) {
                    token = cookie.getValue();
                    cookie.setPath( "/" );
                    cookie.setMaxAge( Consts.USER_INFO_TOKEN_VAI_TIME * 60 );
                    response.addCookie( cookie );
                    return token;
                }
            }
        }

        return null;
    }


}
